﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ChatTool
{
    public partial class MainApplication : Form
    {

        public static DelegateLogEntry dle;
        public delegate void DelegateLogEntry(String value);
        ServerConnection server;
        
        public MainApplication()
        {
            InitializeComponent();
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            String contactName = listBox1.SelectedItem.ToString();
            if (!checkActiveComponents(contactName))
            {
                ChatWindow test = new ChatWindow();
                test.Text = contactName;
                test.Show();
            }
        }

        private bool checkActiveComponents(String contactName)
        {
            return ActiveComponents.ActiveContactChat.Contains(contactName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String username = tb_User.Text;
            String pw = tb_PW.Text;

            server.connectServer(username,pw);
        }

        public void addLogEntry(String text)
        {
            DateTime dt = DateTime.Now;

            if (!InvokeRequired)
            {
                richTextBox1.AppendText(dt.Year + "/" + dt.Month + "/" + dt.Day + " " + dt.ToShortTimeString() + ": " + text);
            }
            else
            {
                Invoke(new DelegateLogEntry(addLogEntry), new object[] { text });
            }
        }

        private void MainApplication_Load(object sender, EventArgs e)
        {
            dle = new DelegateLogEntry(addLogEntry);
            server = new ServerConnection();
        }

    }
}
